package com.hxz.graduationdesign.service.impl;

import com.hxz.graduationdesign.mapper.UserMapper;
import com.hxz.graduationdesign.pojo.entity.User;
import com.hxz.graduationdesign.pojo.vo.UserVo;
import com.hxz.graduationdesign.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.hxz.graduationdesign.util.BilibiliUserCrawler.getUser;

@Service
@Slf4j
public class UserService implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVo queryUser(String uid) {


        UserVo userVo = userMapper.selectByUid(uid);
        if(userVo!=null){
            log.debug("查询用户："+userVo);
            return userVo;
        }

        addUser(uid);
        UserVo userVo1 = userMapper.selectByUid(uid);
        log.debug("查询用户："+userVo1);
        return userVo1;

    }

    @Override
    public int addUser(String uid) {

        User user = getUser(uid);
        log.debug("添加用户："+user);
        return userMapper.insert(user);

}

    @Override
    public UserVo update(String uid) {
        userMapper.delete(uid);
        return queryUser(uid);
    }


}
